//index.js
//获取应用实例
const app = getApp()
var api = require('../../../app/app.js')
Page({
  data: {
    IMG_URL: 'https://www.sdhjfu.com/tooltwo/public/admin/',
    arc: 'https://www.sdhjfu.com/smallhouse/public/house/images/arc.jpg',
    lable: 'https://www.sdhjfu.com/smallhouse/public/house/images/lable.jpg',
    dayu: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/dayu.jpg',
    shareIcon: 'https://www.sdhjfu.com/smallhouse/public/house/images/share.jpg',
    telIcon: 'https://www.sdhjfu.com/smallhouse/public/house/images/telIcon.jpg',
    vertical: true,
    autoplay: true,
    interval: 3500,
    duration: 800,
    userPhone: {},
    houseid: '',
    mapList: {
      lat: '',
      long: '',
      name: '',
      address: ''
    },
    panoramaList: [] // 全景介绍
  },
  onLoad: function (options) { // 获取首页数据
    wx.showLoading({ // 加载数据前的loading
      title: '加载中'
    })
    var that = this
    const shareId = options.houseid // 别人分享的houseid
    var houseid = wx.getStorageSync('houseid') // 同步获取存储houseid
   // console.log(shareId)
   if (shareId !== undefined) {
     that.getData(shareId)
   } else if (houseid == '') {
      // console.log(houseid)
     // console.log('默认页面')
      wx.request({
        url: api.Onelogin,
        method: 'POST', //请求方式
        data: {}, //请求参数格式为json
        header: {
          'content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          //console.log(res.data)
          // 取出首页内容
          that.getData(res.data.houseid)
          wx.setStorageSync('houseid', res.data.houseid) // 本地同步存储
        }
      })
    } else {
      // console.log('点击后的页面')
      that.getData(houseid)
    }
  },
  async  getData(val) { // 异步获取方法
    await this.getHomeData(val)
  },
  // 获取首页的数据
  getHomeData(val) {
    var that = this
    wx.request({
      url: api.Index,
      method: 'POST', //请求方式
      data: {
        houseid: val
      }, //请求参数格式为json
      header: {
        'content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading() // 数据调出后关闭loading
        // console.log(res.data)
        that.setData({
          houseInfo: res.data,
          mapList: {
            lat: res.data.houre.house_lati,
            long: res.data.houre.house_long,
            name: res.data.houre.house_name,
            address: res.data.houre.house_address
          },
          panoramaList: res.data.panorama
        })
        that.data.userPhone = res.data.houre.house_iphone
        wx.setStorageSync('userPhone', that.data.userPhone) // 本地同步存储
      }
    })
  },
  addressClick: function () {
    wx.openLocation({
      latitude: this.data.mapList.house_lati,
      longitude: this.data.mapList.house_long,
      name: this.data.mapList.name,
      address: this.data.mapList.address
    })
  },
  // 跳转项目详情页
  companyClick: function() {
    wx.navigateTo({
      url: '../components/contents/content',
    })
  },
  // 全景介绍预览图片
  panoramaClick(e) {
    const ind = e.currentTarget.dataset.index
    var panoramaLeftImgListCurrent = this.data.IMG_URL + this.data.houseInfo.panorama[ind].pictureimg
    var panoramaLeftImgList = this.data.houseInfo.panorama[ind].twoimg
    var result = []
    for (let i in panoramaLeftImgList) {
      // 这样的写法是result变成数组； +=是变成字符串
      result[i] = this.data.IMG_URL + panoramaLeftImgList[i]
    }
    wx.previewImage({
      current: panoramaLeftImgListCurrent, // 当前显示图片的http链接
      urls: result // 需要预览的图片http链接列表,必须是数组
    })
  },
  // 跳转楼盘图册
  atlasClick: function() {
    wx.navigateTo({
      url: '../components/atlas/atlas',
    })
  },
  // 跳转楼盘新闻
  newsClick: function() {
    wx.navigateTo({
      url: '../components/news/news',
    })
  },
  // 拨打电话
  telClick: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.houseInfo.houre.house_iphone,
    })
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var houseid = wx.getStorageSync('houseid') // 同步获取存储
    let title = this.data.houseInfo.houre.house_name
    if (res.from === 'button') {}
    return {
      title: title,
      path: '/pages/home/index/index?houseid=' + houseid,
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})